// 整数
let findEle = (parent, type) => {
    return parent.tagName.toLowerCase() === type
        ? parent
        : parent.querySelector(type)
}

const trigger = (el, type) => {
    const e = document.createEvent('HTMLEvents')
    e.initEvent(type, true, true)
    el.dispatchEvent(e)
}

const integer = {
    bind: function (el, binding, vnode) {
        console.log(binding, vnode)
        var regRule = /^\+?[1-9]\d*$/ // /[^\u4E00-\u9FA5|\d|\a-zA-Z|\r\n\s,.?!，。？！…—&$=()-+/*{}[\]]|\s/g
        let $inp = findEle(el, 'input')
        el.$inp = $inp
        $inp.handle = function () {
            let val = $inp.value
            if (regRule.test($inp.value)) {
                $inp.value = val
            } else {
                $inp.value = ''
            }
            trigger($inp, 'input')
        }
        $inp.addEventListener('keyup', $inp.handle)
    },
    unbind: function (el) {
        el.$inp.removeEventListener('keyup', el.$inp.handle)
    }
}

export default integer